package com.jxs.android.weiget;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.util.Log;

import com.telecom.weixi.R;

public class SimpleWSDate {
	int mYear;
	int mMonth;
	int mDay;
	
	public int getYear(){
		return mYear;
	}
	public int getMonth(){
		return mMonth;
	}
	public int getDay(){
		return mDay;
	}
	
	public SimpleWSDate addValueMonth(int value){
		Calendar ca = Calendar.getInstance();   
        ca.set(mYear, mMonth-1, mDay);  
        ca.add(Calendar.MONTH, value);      
        Date lastDate = ca.getTime();     
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        return new SimpleWSDate(sf.format(lastDate));
	}
	public SimpleWSDate addValueDay(int value){
		Calendar ca = Calendar.getInstance();   
        ca.set(mYear, mMonth-1, mDay);  
        ca.add(Calendar.DATE, value);      
        Date lastDate = ca.getTime();     
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        return new SimpleWSDate(sf.format(lastDate));
	}
	public String toMMDD(){
		String strMonth = mMonth < 10 ? "0"+String.valueOf(mMonth) : String.valueOf(mMonth);
		String strDay = mDay < 10 ? "0" + String.valueOf(mDay) : String.valueOf(mDay);
		return strMonth+strDay;
	}
	public String toYYMM(){
		String strYear = String.valueOf(mYear).substring(2, 4);
		String strMonth = mMonth < 10 ? "0"+String.valueOf(mMonth) : String.valueOf(mMonth);
		return strYear + strMonth;
	}

	public SimpleWSDate(String ymd){
		if(ymd.length() == 8){ //YYYYMMDD
			String strYear = ymd.substring(0, 4);
			mYear = Integer.valueOf(strYear);
			String strMonth = ymd.substring(4, 6);
			mMonth = Integer.valueOf(strMonth);
			String strDay = ymd.substring(6, 8);
			mDay = Integer.valueOf(strDay);
		}else if(ymd.length() == 6){	//YYYYMM
			String strYear = ymd.substring(0, 4);
			mYear = Integer.valueOf(strYear);
			String strMonth = ymd.substring(4, 6);
			mMonth = Integer.valueOf(strMonth);
			mDay = 1;
		}else{
			Log.e("test", "must be YYYYMMDD fromat");
		}
	}
	public SimpleWSDate(int year, int month, int day){
		mYear = year;
		mMonth = month;
		mDay = day;
	}
	
	public String toString(){
		String strMonth = mMonth < 10 ? "0"+String.valueOf(mMonth) : String.valueOf(mMonth);
		String strDay = mDay < 10 ? "0" + String.valueOf(mDay) : String.valueOf(mDay);
		
		return String.valueOf(mYear) + strMonth + strDay;
	}
	public String toMD(Context context){
		String strMonth = mMonth < 10 ? "0"+String.valueOf(mMonth) : String.valueOf(mMonth);
		String strDay = mDay < 10 ? "0" + String.valueOf(mDay) : String.valueOf(mDay);
		return strMonth + context.getString(R.string.prompt_month) + strDay + context.getString(R.string.prompt_day);
	}
	public String toYM(Context context){
		String strYear = String.valueOf(mYear).substring(2, 4);
		String strMonth = mMonth < 10 ? "0"+String.valueOf(mMonth) : String.valueOf(mMonth);
		return strYear + context.getString(R.string.prompt_year) +strMonth + context.getString(R.string.prompt_month);
	}
	public String toYMD(Context context){
		String strYear = String.valueOf(mYear);
		String strMonth = mMonth < 10 ? "0"+String.valueOf(mMonth) : String.valueOf(mMonth);
		String strDay = mDay < 10 ? "0" + String.valueOf(mDay) : String.valueOf(mDay);
		return strYear + context.getString(R.string.prompt_year) + 
			strMonth + context.getString(R.string.prompt_month) + 
			strDay + context.getString(R.string.prompt_day);
	}
	
	public int compareTime(SimpleWSDate compare){
		int compYear = this.mYear - compare.mYear;
		if(compYear != 0){
			return compYear;
		}
		int compMonth = this.mMonth - compare.mMonth;
		if(compMonth != 0){
			return compMonth;
		}
		return this.mDay - compare.mDay;
	}
}